﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Common;
using ESOL.Common.Entity;
using ESOL.Business.Mst;
using ESOL.Business.Proj;
using ESOL.WinUI.Bpt;
using ESOL.Business.Prod;

namespace ESOL.WinUI.Proj
{
    public partial class frmManageMBOM : BaseForm.frmBaseMdiBusinessForm
    {
        #region vars
        public MstProductEntity SelectedProduct { set; get; }
        public ProdPlanOrderEntity SelectedPlanOrder { set; get; }

        private List<string> Listcondition = new List<string>();
        private List<string> ListProductId = new List<string>();
        #endregion

        #region ctors
        public frmManageMBOM()
        {
            InitializeComponent();
        }
        #endregion

        #region functions
        private void SetButton()
        {
            switch (MBOMSelected.ApproveStatus)
            {
                case 0:
                    btnModify.Enabled = true;
                    btnDelete.Enabled = true;
                    btnSubmit.Enabled = true;
                    btnApprove.Enabled = false;
                    btnActive.Enabled = false;
                    btnExport.Enabled = true;
                    btnApply.Enabled = false;
                    break;
                case 1:
                    btnModify.Enabled = false;
                    btnDelete.Enabled = false;
                    btnSubmit.Enabled = false;
                    btnApprove.Enabled = true;
                    btnActive.Enabled = false;
                    btnExport.Enabled = true;
                    btnApply.Enabled = false;
                    break;
                case 2:
                    btnModify.Enabled = false;
                    btnDelete.Enabled = false;
                    btnSubmit.Enabled = false;
                    btnApprove.Enabled = false;
                    btnActive.Enabled = true;
                    btnExport.Enabled = true;
                    if (SelectedPlanOrder != null && MBOMSelected.Status.Equals("BOM_S00"))
                    {
                        btnApply.Enabled = true;
                    }
                    else
                    {
                        btnApply.Enabled = false;
                    }
                    break;
                case 3:
                    btnModify.Enabled = true;
                    btnDelete.Enabled = true;
                    btnSubmit.Enabled = true;
                    btnApprove.Enabled = false;
                    btnActive.Enabled = false;
                    btnExport.Enabled = true;
                    btnApply.Enabled = false;
                    break;
                case 4:
                    btnModify.Enabled = false;
                    btnDelete.Enabled = false;
                    btnSubmit.Enabled = false;
                    btnApprove.Enabled = false;
                    btnActive.Enabled = false;
                    btnExport.Enabled = false;
                    btnApply.Enabled = false;
                    break;
            }
            
        }

        private void BindCombobox()
        {
            UICommon.UICommonFunctions.BindComboboxByEnum(cmbStatus, typeof(BomApproveStatus), typeof(int), true);

            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(rcolItemTypeMain, "PDTYPE", false);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(rcolValidStatusMain, "BOM_S", false);
            UICommon.UICommonFunctions.BindGridComboboxByEnum(rcolCustomerForMain,typeof(IsCustomerForFlag), typeof(int),false);
            UICommon.UICommonFunctions.BindGridComboboxByEnum(rcolApproveStatusMain, typeof(BomApproveStatus), typeof(int), false);

            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(rcolItemTypeDetail, "PDTYPE", false);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(rcolValidStatusDetail, "BOM_S", false);
            UICommon.UICommonFunctions.BindGridComboboxByEnum(rcolCustomerForDetail, typeof(IsCustomerForFlag), typeof(int), false);
            UICommon.UICommonFunctions.BindGridComboboxByEnum(rcolApproveStatusDetail, typeof(BomApproveStatus), typeof(int), false);

        }

        private string GetCondition()
        {
            string condition = "";
            if (string.IsNullOrEmpty(txtProductCode.Text) == false)
            {
                condition += " and bpbm.PRODUCT_ID='" + SelectedProduct.ProductId + "'";
            }
            if (string.IsNullOrEmpty(cmbStatus.SelectedValue.ToString()) == false)
            {
                condition += " and bpbm.APPROVE_STATUS='" + cmbStatus.SelectedValue.ToString() + "'";
            }
            return condition;
        }

        private void SetField()
        {
            if (SelectedProduct == null)
            {
                return;
            }
            txtProductCode.Text = SelectedProduct.ProductCode;
        }

        private void BindGridMain()
        {
            string sCondition = GetCondition();
            tlMain.DataSource = ProjMBOMBs.GetDtProductBomByCondition(sCondition);
        }

        private void BindGridDetail()
        {
            if (MBOMSelected == null)
            {
                return;
            }
            string sBomStatus = cmbStatus.SelectedValue.ToString();
            DataTable dt = ProjMBOMBs.GetDtProductBomByBomID(MBOMSelected.BomId, sBomStatus, "");
            tlDetail.DataSource = dt;
            tlDetail.ExpandAll();
        }

        private bool ApplyBom()
        {
            SelectedPlanOrder.BomId = MBOMSelected.BomId;
            if (ProdPlanOrderBs.UpdateProdPlanOrder(SelectedPlanOrder)==false)
            {
                return false;
            }
            return true;
        }
        #endregion

        #region events
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            BindCombobox();
            SetField();
            BindGridMain();
        }

        private void txtProductCode_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainProductQuery frm = new frmMaintainProductQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedProduct != null)
            {
                txtProductCode.Text = frm.SelectedProduct.ProductCode;
                SelectedProduct = frm.SelectedProduct;
            }
            else
            {
                txtProductCode.Text = "";
                SelectedProduct = null;
            }
        }

        protected override void Click_Query()
        {
            BindGridMain();
        }

        private BomProductBomMasterEntity MBOMSelected;
        private void tlMain_AfterFocusNode(object sender, DevExpress.XtraTreeList.NodeEventArgs e)
        {
            if (tlMain.DataSource == null)
            {
                return;
            }
            DataTable dt = tlMain.DataSource as DataTable;
            MBOMSelected = new BomProductBomMasterEntity(dt.Rows[e.Node.Id]);
            SelectedProduct = MstProductBs.GetMstProductTypeByID(dt.Rows[e.Node.Id]["PRODUCT_ID"].ToString());
            SetButton();
        }

        protected override void Click_View()
        {
            if (MBOMSelected == null)
            {
                MessageBox.Show("请选择要修改的物料！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainMBOM frm = new frmMaintainMBOM();
            frm.SelectedProduct = SelectedProduct;
            frm.SelectedProductBomMaster = MBOMSelected;
            frm.ThisActionType = ActionType.View;
            frm.ShowDialog();
        }

        protected override void Click_New()
        {
            frmMaintainMBOM frm = new frmMaintainMBOM();
            frm.SelectedProduct = SelectedProduct;
            frm.ThisActionType = ActionType.New;
            if (frm.ShowDialog() == DialogResult.OK)
            {
                BindGridMain();
            }
        }

        protected override void Click_Modify()
        {
            if (MBOMSelected == null)
            {
                MessageBox.Show("请选择要修改的物料！","Information",MessageBoxButtons.OK,MessageBoxIcon.Information);
                return;
            }
            DataTable dt = ProdPlanOrderItemBs.GetAllByBomID(MBOMSelected.BomId);
            if (dt.Rows.Count > 0)
            {
                MessageBox.Show("物料清单已经被工令单引用，请修改版本新增！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainMBOM frm = new frmMaintainMBOM();
            frm.SelectedProduct = SelectedProduct;
            frm.SelectedProductBomMaster = MBOMSelected;
            frm.ThisActionType = ActionType.Modify;
            if (frm.ShowDialog() == DialogResult.OK)
            {
                BindGridMain();
            }
        }

        protected override void Click_Delete()
        {
            if (MBOMSelected == null)
            {
                MessageBox.Show("不存在物料清单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DataTable dt = ProdPlanOrderItemBs.GetAllByBomID(MBOMSelected.BomId);
            if (dt.Rows.Count > 0)
            {
                MessageBox.Show("物料清单已经被工令单引用，不允许删除！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (MessageBox.Show("确认删除？", "Information", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                if (ProjMBOMBs.DeleteBomProductBomMaster(MBOMSelected))
                {
                    MessageBox.Show("删除成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindGridMain();
                    return;
                }
                else
                {
                    MessageBox.Show("删除失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
        }

        protected override void Click_Submit()
        {
            if (MBOMSelected == null)
            {
                MessageBox.Show("不存在物料清单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            MBOMSelected.ApproveStatus = (int)BomApproveStatus.审核中;
            if (ProjMBOMBs.UpdateBomProductBomMaster(MBOMSelected))
            {
                MessageBox.Show("送审成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                BindGridMain();
                return;
            }
            else
            {
                MessageBox.Show("送审失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
        }

        protected override void Click_Approve()
        {
            if (MBOMSelected == null)
            {
                MessageBox.Show("不存在物料清单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainMBOM frm = new frmMaintainMBOM();
            frm.SelectedProductBomMaster = MBOMSelected;
            frm.ThisActionType = ActionType.Approve;
            if (frm.ShowDialog() == DialogResult.OK)
            {
                BindGridMain();
            }
        }

        //激活/关闭
        private void btnActive_Click(object sender, EventArgs e)
        {
            if (MBOMSelected == null)
            {
                MessageBox.Show("不存在物料清单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (MBOMSelected.Status.Equals("BOM_S00"))
            {
                MBOMSelected.Status = "BOM_S01";
            }
            else
            {
                MBOMSelected.Status = "BOM_S00";
            }
            if (ProjMBOMBs.UpdateBomProductBomMaster(MBOMSelected))
            {
                DataTable dt = tlMain.DataSource as DataTable;
                DataRow[] drs = dt.Select("BOM_ID='" + MBOMSelected.BomId + "'");
                drs[0]["STATUS"] = MBOMSelected.Status;
            }
            else
            {
                MessageBox.Show("更新状态失败!","Information",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
        }

        protected override void Click_Export()
        {
            SaveFileDialog frm = new SaveFileDialog();
            frm.Filter = "All Files (*.*)|";
            frm.DefaultExt = "xls";
            frm.AddExtension = true;
            frm.FileName = "物料清单导出列表" + DateTime.Today.ToString("yyyy-MM-dd");
            if (frm.ShowDialog() == DialogResult.OK)
            {
                tlDetail.ExportToXls(frm.FileName);
            }    
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            if (SelectedPlanOrder == null)
            {
                MessageBox.Show("生产计划单为空，无法引用", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (MBOMSelected == null)
            {
                MessageBox.Show("物料清单为空，无法引用", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (ApplyBom())
            {
                MessageBox.Show("引用成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("引用失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void tlMain_Click(object sender, EventArgs e)
        {
            if (MBOMSelected == null)
            {
                return;
            }
            BindGridDetail();
        }
        #endregion

    }
}
